﻿
function load(t) {

    $("." + t + " input[id$=txtSort_" + t + "]").each(function() {
        loadfiled(this, "txtSort_" + t, t);
    });
}

function loadfiled(ele, id, t, c) {
    var idPrefix = $(ele).attr("id").replace(id, "");
    
    //var namePrefix = $(ele).attr("name").replace(id, "");

    var fid = $("#" + idPrefix + "lblID").text();
    var fontFamily = $("#" + idPrefix + "dropFontFamily_" + t).val();
    var color = c ? c : $("#" + idPrefix + "txtColor_" + t).val();
    color = color.replace("#", "");
    var fontSize = $("#" + idPrefix + "txtFontSize_" + t).val();
    var top = $("#" + idPrefix + "txtTop_" + t).val();
    var left = $("#" + idPrefix + "txtLeft_" + t).val();
    var direction = $("#" + idPrefix + "rdbDirection_" + t + "_0").is(":checked")?"R":"L";
    var content = escape($("#" + idPrefix + "txtContent_" + t).val());

    var img = $("#" + fid + "_img").length <= 0 ? ($("<img style='border: 0px; z-index: 99;position:absolute' id='" + fid + "_img' />").appendTo($("#li_card_" + t)))
               : $("#" + fid + "_img");

    var url = imgServer + "ImageService.ashx?a=aw" + "&c=" + color + "&ff=" + fontFamily + "&fs=" + fontSize + "&w=" + content;

    if (direction == "R") {
        img.css('top', top + 'px')
        .css('left', left + 'px');
    } else {
        imgReady(url, function() {
            img.css('top', top + 'px')
                   .css('left', (left - this.width) + 'px')
        });
    }

    if ((version >= 5.5) && (version < 7)) {
        img[0].onreadystatechange = function() {
            if (img[0].readyState == "complete" && img[0].src.indexOf("transparent.gif") <= -1) {
                img[0].runtimeStyle.backgroundImage = "none";
                img[0].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img[0].src + "', sizingMethod='image')";
                img[0].src = "transparent.gif";
            }
        }
    }

    img.attr("src", url);
}

function settingLogo(t, input) {
    if (validateLogoCSSInput($(input))) {
        var logo = $("#" + t + "LImg");
        if (logo.length > 0) {
            logo.css("width", $("#txtLogoWidth" + t).val() + "px");
            logo.css("height", $("#txtLogoHeight" + t).val() + "px");
            logo.css("top", $("#txtLogoTop" + t).val() + "px");
            logo.css("left", $("#txtLogoLeft" + t).val() + "px");
        }
    }
}

function validateLogoCSSInput(input) {
    if (isNaN(input.val())) {
        input.val(input.attr("h"));
        return false;
    } else {
        input.attr("h", input.val());
    }
    return true;
}

function resetLogo(width, height, logo, t) {
    $("#txtLogoWidth" + t).val(width);
    $("#txtLogoHeight" + t).val(height);
    $("#txtLogo" + t).val(logo);
    settingLogo(t, $("#txtLogoWidth" + t)[0]);
    $("#" + t + "LImg").attr("src", imgServer + "/logoImages/" + logo);
    $("#" + t + "Logo").attr("src", imgServer + "/logoImages/" + logo);
}

function picklogo(t, callback) {
    if ($("#logo-picker").length <= 0) {
        $(document.forms[0]).append("<div id='logo-picker' style='display:none;'><iframe src='LogoPicker.aspx?ft=" + t + "' scrolling='no' frameborder=no style='border:0;width:380px;height:390px;background-color:#EEEEEE;' /></div>");
    }

    $("#logo-picker").dialog({
        close: function() {
            $("#logo-picker").dialog("destroy");
            $("#logo-picker").remove();
            callback ? callback.call() : "";
        },
        width: 430,
        height: 460,
        modal: true
    });
}

function closePicklogo() {
    $("#logo-picker").dialog("close");
}
